#ifndef __BITCONVERTER_H__
#define __BITCONVERTER_H__


#include <stdint.h>
#include "plat/nonecopyable.h"


/// @brief 
class BitConverter : public NoneCopyable
{
public:
    /// @brief 数值类型转换
    /// @tparam T 
    /// @param buffer 
    /// @param offset 
    /// @return 
    template<typename T>
    inline static T ToValue(uint8_t *buffer, int offset = 0)
    {
        T value;

        memcpy((uint8_t *)&value, buffer + offset, sizeof(T));
        return value;
    }
    
    template<typename T>
    inline static void ToBytes(T t, uint8_t *buffer, int offset = 0)
    {
        memcpy(buffer + offset, (uint8_t *)&t, sizeof(T));
    }

    template<typename T>
    inline static uint8_t GetByte(T t, int indexByte)
    {
        return (t >> indexByte * 8) & 0xFF;
    }

    template<typename T>
    inline static uint8_t SetByte(T t, int indexByte)
    {
        return ((t | (1u << (8*indexByte))));
    }

    template<typename T>
    inline static void SetBit(T t, int indexBit)
    {
        return (t | (1u << indexBit));
    }

    template<typename T>
    inline static void ClearBit(T t, int indexBit)
    {
        return (t & (~(1u << indexBit)));
    }

    template<typename T>
    inline static void CplBit(T t, int indexBit)
    {
        return (t ^ (1u << indexBit));
    }

    template<typename T>
    inline static void GetBit(T t, int indexBit)
    {
        return  ((t & (1u << indexBit)) != 0);
    }

    /// @brief 转换为string
    /// @param buffer 
    /// @param offset 
    /// @param count 
    /// @return 
    static std::string ToString(uint8_t *buffer, int offset = 0, int count = -1);
};

#endif // __BITCONVERTER_H__
